package com.app.bimo.commonbusiness.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;

import com.app.bimo.commonbusiness.R;


/**
 * 滚动的textView 自定义了两个属性refresh_rate和per_distance。
 * refresh_rate：刷新的频率（单位ms）
 * per_distance：每刷新一次文字移动的距离
 *
 * @author flywoniu
 */
@SuppressLint("AppCompatCustomView")
public class ScrollContentTextView extends TextView {

    private Paint mPaint;
    Rect mBounds;
    float mX = 0;

    String contentTextStr = "";

    boolean isFirstDraw = true;

    int mRate = 0;

    float per_distance = 0;

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            changemX();
            invalidate();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    handler.sendEmptyMessage(0);
                }
            }, mRate);
        }

        private void changemX() {
            if (mX + mBounds.width() > 0) {
                mX -= per_distance;
            } else {
                mX = mBounds.width();
            }
        }
    };

    public ScrollContentTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        getAttr(context, attrs);

        mPaint = new Paint();
        mBounds = new Rect();
        contentTextStr = getText().toString();
        mPaint.setTextSize(getTextSize());
        mPaint.setColor(getCurrentTextColor());
        //
        mPaint.getTextBounds(contentTextStr, 0, contentTextStr.length(),
                mBounds);

    }

    private void getAttr(Context context, AttributeSet attrs) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.custom);

        int n = typedArray.getIndexCount();

        for (int i = 0; i < n; i++) {
            int attr = typedArray.getIndex(i);
            if (attr == R.styleable.custom_refresh_rate) {
                mRate = typedArray.getInt(attr, 100);

            } else if (attr == R.styleable.custom_per_distance) {
                per_distance = typedArray.getFloat(attr, 1);

            }
        }
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // super.onDraw(canvas);
        drawText(canvas);
    }

    void drawText(Canvas canvas) {

        if (isFirstDraw) {
            mX = getMeasuredWidth();
            canvas.drawText(contentTextStr, 0, contentTextStr.length(), mX,
                    getHeight() / 2 + mBounds.height() / 2,// x为左边，y为文本底部开始
                    mPaint);
            handler.sendEmptyMessage(0);
            isFirstDraw = false;
        } else {
            canvas.drawText(contentTextStr, 0, contentTextStr.length(), mX,
                    getHeight() / 2 + mBounds.height() / 2,// x为左边，y为文本底部开始
                    mPaint);
        }
    }

}
